<?php
	class Venue{

		function __construct(){
		}

		function __destruct(){
		
		}

		/* This function is used to get all services
		@Input : interger $id
		@Output : array $services
		*/
		public function getVenueTariff($id = 0){
			$where = " WHERE vtf.status != '2' ";
			if(!empty($id)) $where.=" AND vtf.id =".$id;
			$qry = "SELECT vtf.*, vd.name as vendor_name, vn.venue_name as venue_name FROM venue_tariff as vtf 
			LEFT JOIN vendor as vd ON vtf.vendor_id = vd.id
			LEFT JOIN venues as vn ON vtf.venue_id = vn.id
			".$where." ORDER BY vtf.added ASC";
			$rs = MyDB::$db->Execute($qry);
			$services = array();
			while ($row = $rs->fetchRow()) {
				$services[] = $row;
			}
			return $services;
		}
		
		
		/* This function is used to get all venues
		@Input : interger $id
		@Output : array $venues
		*/
		public function getVenues($id = 0){
			$where = " WHERE v.status != '2' ";
			if(!empty($id)) $where.=" AND v.id =".$id;
			$qry = "SELECT * FROM venues as v ".$where;
			$rs = MyDB::$db->Execute($qry);
			$venues = array();
			while ($row = $rs->fetchRow()) {
				$venues[] = $row;
			}
			return $venues;
		}
		
		/* This function is used to get all venues
		@Input : interger $id
		@Output : array $venues
		*/
		public function getVenueTypes($id = 0){
			$where = " WHERE vtm.status != '2' ";
			if(!empty($id)) $where.=" AND vtm.id =".$id;
			$qry = "SELECT * FROM venue_type_master as vtm ".$where." ORDER BY vtm.added ASC";
			$rs = MyDB::$db->Execute($qry);
			$venuetypes = array();
			while ($row = $rs->fetchRow()) {
				$venuetypes[] = $row;
			}
			return $venuetypes;
		}
		
		/* This function is used to get all venues
		@Input : interger $id
		@Output : array $venues
		*/
		public function getActiveVenueTypes($id = 0){
			$status  =1;
			$where = " WHERE vtm.status = ".MyDB::$db->qstr($status);
			if(!empty($id)) $where.=" AND vtm.id =".$id;
			$qry = "SELECT vtm.id, vtm.venue_type FROM venue_type_master as vtm ".$where." ORDER BY vtm.venue_type ASC";
			$rs = MyDB::$db->Execute($qry);
			$venuetypes = array();
			while ($row = $rs->fetchRow()) {
				$venuetypes[] = $row;
			}
			return $venuetypes;
		}
		
		/* This function is used to get all venues
		@Input : interger $id
		@Output : array $venues
		*/
		public function getActiveVenueServices($id = 0){
			$status  =1;
			$where = " WHERE vsm.status = ".MyDB::$db->qstr($status);
			if(!empty($id)) $where.=" AND vsm.id =".$id;
			$qry = "SELECT vsm.id, vsm.service FROM venue_service_master as vsm ".$where." ORDER BY vsm.service ASC";
			$rs = MyDB::$db->Execute($qry);
			$venueservices = array();
			while ($row = $rs->fetchRow()) {
				$venueservices[] = $row;
			}
			return $venueservices;
		}
		
		/* This function is used to get all venues
		@Input : interger $id
		@Output : array $venues
		*/
		public function getSelectedVenueServices($id = 0){
			$status  =1;
			$where = " WHERE vs.status = ".MyDB::$db->qstr($status);
			if(!empty($id)) $where.=" AND vs.venue_id =".$id;
			$qry = "SELECT vs.* FROM venue_services as vs ".$where;
			$rs = MyDB::$db->Execute($qry);
			$venueservices = array();
			while ($row = $rs->fetchRow()) {
				array_push($venueservices, $row['service_id']);
			}
			return $venueservices;
		}
		
		/* This function is used to get all venues
		@Input : interger $id
		@Output : array $venues
		*/
		public function getActiveVenues($id = 0){
			$status  =1;
			$where = " WHERE v.status = ".MyDB::$db->qstr($status);
			if(!empty($id)) $where.=" AND v.id =".$id;
			$qry = "SELECT v.id, v.venue_name FROM venues as v ".$where." ORDER BY v.venue_name ASC";
			$rs = MyDB::$db->Execute($qry);
			$venues = array();
			while ($row = $rs->fetchRow()) {
				$venues[] = $row;
			}
			return $venues;
		}
		
		/* This function is used to get all services
		@Input : interger $id
		@Output : array $services
		*/
		public function getVenueServices($id = 0){
			$where = " WHERE vsm.status != '2' ";
			if(!empty($id)) $where.=" AND vsm.id =".$id;
			$qry = "SELECT * FROM venue_service_master as vsm ".$where." ORDER BY vsm.added ASC";
			$rs = MyDB::$db->Execute($qry);
			$services = array();
			while ($row = $rs->fetchRow()) {
				$services[] = $row;
			}
			return $services;
		}
		
		/* This function is used to get all services
		@Input : interger $id
		@Output : array $services
		*/
		public function getVenueRatings($id = 0){
			$where = " WHERE vrm.status != '2' ";
			if(!empty($id)) $where.=" AND vrm.id =".$id;
			$qry = "SELECT * FROM venue_rating_master as vrm ".$where." ORDER BY vrm.added ASC";
			$rs = MyDB::$db->Execute($qry);
			$ratings = array();
			while ($row = $rs->fetchRow()) {
				$ratings[] = $row;
			}
			return $ratings;
		}


		public function getVenueInventories($id = 0){
			$where = " WHERE vin.status != '2' ";
			if(!empty($id)) $where.=" AND vin.id =".$id;
			$qry = "SELECT vin.*, v.display_name FROM venue_inventory as vin INNER JOIN venues as v ON v.id = vin.venue_id ".$where." ORDER BY vin.added ASC";
			$rs = MyDB::$db->Execute($qry);
			$inventories = array();
			while ($row = $rs->fetchRow()) {
				$inventories[] = $row;
			}
			return $inventories;
		}
		
		
		// Front End Queries	
	public function getVenue($limit='',$param='') { 
		
		//MyDB::$db->debug = true;
		if($param['country'])
			$and .= " AND V.country = ".(int)$param['country'];
		if($param['city'])
			$and .= " AND V.city = ".(int)$param['city'];
		if($param['wcategory'])
			$and .= " AND V.venue_type = ".(int)$param['wcategory'];
		$order_by = ($param['sort_venue'] != '')? " ORDER BY V.".$param['sort_venue']:' ORDER BY V.booking_amount ';
		$sql ="SELECT * FROM venues V inner join venue_tariff VT on(V.id = VT.venue_id) WHERE VT.status=1 ".$and.$order_by.$limit;
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		$rset = MyDB::$db->GetArray($sql);
		
		return $rset;
	}
	
	public function getVenueCount($param='') { 
		if($param['country'])
			$and .= " AND V.country = ".(int)$param['country'];
		if($param['city'])
			$and .= " AND V.city = ".(int)$param['city'];
		if($param['wcategory'])
			$and .= " AND V.venue_type = ".(int)$param['wcategory'];
		$sql ="SELECT count(*) FROM venues V  inner join venue_tariff VT on(V.id = VT.venue_id) WHERE VT.status=1 ".$and;
		return MyDB::$db->GetOne($sql);
	
	}
	
	public function getVenueDetail($id=NULL) { 
		
		//MyDB::$db->debug = true;
		$sql ="SELECT * FROM venues V inner join venue_tariff VT on(V.id = VT.venue_id) WHERE VT.status=1 AND VT.id = ".(int)$id;
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		$rset = MyDB::$db->GetArray($sql);
		return $rset;
	}
	
	public function getVenueRandom($id=NULL,$count=1) {
		
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		$query = "SELECT * FROM venues where id !=".(int)$id." order by rand() LIMIT $count";
		return MyDB::$db->GetArray($query);
		
	}
	
	public function getVenueType($param='') {
		if($param['wcategory'])
			$and =" AND id =".(int)$param['wcategory'];
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		$query = "SELECT id, venue_type AS value FROM venue_type_master where status = 1 $and order by venue_type";
		return MyDB::$db->GetArray($query);
	}
	
	public function venueServices($ids=''){
		
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		$query = "SELECT vs.*, sm.name AS service_name, v.name vendor_name FROM vendor_services AS vs INNER JOIN vendor AS v ON(vs.vendor_id = v.id) INNER JOIN services_master AS sm ON(vs.service_id = sm.id) WHERE vs.id IN(".$ids.") AND sm.status='1' AND v.status='1'";
		
		
		return MyDB::$db->GetArray($query);
	}
	
}	
?>